﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using Dict.Blo;

namespace UI
{
    public class ItemIcon : MonoBehaviour
    {
        public TextureKit itemIcon;
        public UILabel itemCount;
        public UISprite nature;
        public GameObject chosedImg;
        public GameObject widgetForBag;
        string m_itemId;
        public string ItemId
        {
            get
            {
                return m_itemId;
            }
        }
            
        public delegate void ItemClickCallback(string itemId);
        public ItemClickCallback m_callback;

        System.Diagnostics.Stopwatch stopWatch;
        long lastRmbTime;
        bool ignorePress;
        bool donotCallback;

        void Awake()
        {
            itemCount.gameObject.SetActive(false);
            lastRmbTime = 0;
            ignorePress = true;
            donotCallback = false;
        }

        public void SetItem(string id,bool showNatrue=false)
        {
            m_itemId = id;
            
            TableItem itemData = DictItemBlo.GetTableItem(id);

            if (itemData == null)
            {
                if (id == "coin")
                {
                    itemIcon.textureName = "item_icon_200";
                }
                else if (id == "diamond")
                {
                    itemIcon.textureName = "item_icon_201";
                }
                else if (id == Dict.Config.TableConst.popularity)
                {
                    itemIcon.textureName = Dict.Config.TableConst.popularity;
                }
                else if (id == Dict.Config.TableConst.score)
                {
                    itemIcon.textureName = Dict.Config.TableConst.score;
                }
                return;
            }
            else
            {
                itemIcon.textureName = itemData.icon;
                if(showNatrue)
                {
                    nature.gameObject.SetActive(true);
                    nature.spriteName = GetNatureSprName(itemData.nature);
                }
                else
                    nature.gameObject.SetActive(false);
            }
        }

        string GetNatureSprName(int nature)
        {
            switch(nature)
            {
                case 1: return "UI_chongwu_item_huo";
                case 2: return "UI_chongwu_item_feng";
                case 3: return "UI_chongwu_item_shui";
                case 4: return "UI_chongwu_item_guang";
                case 5: return "UI_chongwu_item_an";
                default:return "";
            }
        }
        public void UpdateItemCount(string text, UIWidget.Pivot pivot = UIWidget.Pivot.Center)
        {
            itemCount.gameObject.SetActive(true);
            itemCount.pivot = pivot;
            itemCount.text = text;
        }

        public void OnClick()
        {
            if (donotCallback)
            {
                return;
            }
            if (m_callback != null)
            {
                AudioManager.Instance.PlayUIAudio("buttonClick");
                Debug.Log("callback in click");
                m_callback(m_itemId);
            }
        }

        public void StopStopWatch()
        {
            if (stopWatch != null)
            {
                donotCallback = true;
                stopWatch.Stop();
            }
        }

        public void IgnorePress(bool value)
        {
            ignorePress = value;
        }

        public void OnPress(bool press)
        {
            if (m_callback == null || ignorePress)
            {
                return;
            }

            if (press)
            {
                donotCallback = false;
                lastRmbTime = 0;
                if(stopWatch == null)
                {
                    stopWatch = new System.Diagnostics.Stopwatch();
                }
                stopWatch.Reset();
                stopWatch.Start();
                Debug.Log("press down");
            }
            else
            {
                stopWatch.Stop();
                Debug.Log("press up");
            }
        }

        void Update()
        {
            if(stopWatch != null && stopWatch.IsRunning)
            {
                if(stopWatch.ElapsedMilliseconds - lastRmbTime >= 100)
                {
                    lastRmbTime = stopWatch.ElapsedMilliseconds;

                    if (m_callback != null)
                    {
                        //Debug.Log("callback in press");
                        m_callback(m_itemId);
                    }
                }
            }
        }
    }
}
